package com.dong.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dong.beans.staticBeaned;
import com.dong.mqtt.EmqClient;
import com.dong.mqtt.EmqMsgProcess;
import com.dong.mybatis.entity.Zone1;
import com.dong.mybatis.service.impl.Zone1ServiceImpl;
import com.dong.staticdata.data;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RequestMapping("mqtt")
@RestController
@Slf4j
public class MqttController {
@Resource
Zone1ServiceImpl zone1Service;

    @Resource
    EmqMsgProcess emqMsgProcess;

    @GetMapping("/getmsg")
    public String getMes() {
//    String  msg=  String.valueOf(staticBeaned.light)+String.valueOf(staticBeaned.voltage)+String.valueOf(staticBeaned.tempHum);
//        data a=new data();
//        return emqMsgProcess.msg1;
        return  null;
    }

    @Autowired
    EmqClient emqClient;
    @PostMapping("postmsg")
    public String postMes(String topic, String message) {
        log.info("接收到❤️控制数据{}来自主题{}",message,topic);
//        emqMsgProcess.
       emqClient.publish(topic,message);

        return null;
    }

    // 查询最新时间的数据
    @GetMapping("/latest")
    public Zone1 getLatestData() {
        // 构建查询条件：按 time 倒序，取第一条
        LambdaQueryWrapper<Zone1> queryWrapper = new LambdaQueryWrapper<Zone1>()
                .orderByDesc(Zone1::getTime) // 按时间降序（最新的在前）
                .last("LIMIT 1"); // 只取第一条（MySQL 语法）

        // 调用 MyBatis-Plus 自带的 selectOne 方法
        return zone1Service.getOne(queryWrapper);
    }


}